<?php
if (!function_exists('htmlspecialchars_decode'))
{
	function htmlspecialchars_decode($text) {
		return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
	}
}

include_once 'facebook_conf.php';
include_once '../library/facebook/facebookapi_php4_restlib.php';

// get username
$username = (isset($_POST['username'])?strip_tags($_POST['username']):'');

//if they are not logged in, send them to the login page.
if (empty($_COOKIE['facebook_session_key']) || empty($_COOKIE['facebook_uid'])) {
	$cookiesSet = array_keys($_COOKIE);
	for ($x=0;$x<count($cookiesSet);$x++) setcookie($cookiesSet[$x],"",time()-1);
	//they can't login if they are still logged into facebook, so
	print"<html><head><title>fl2fb</title><style type='text/css'>p { font-family: verdana, arial; text-align: center; font-size: 14px; }</style></head><body><p style='text-align: center'><strong>Welcome to fl2fb, one way photo album syncing between Flickr and Facebook</strong></p>
	<!-- <br><a href=\"javascript:logout()\">If you are logged into Facebook, please log out of Facebook first</a></font>
<form name=\"logout\" method=\"post\" action=\"http://www.facebook.com/logout.php\"> 
  <input type=\"hidden\" name=\"confirm\" value=\"1\"/>
</form> 
<script type=\"text/javascript\">
	function logout() {
		document.cookie='facebook_session_key=;expires=Thu, 01-Jan-1970 00:00:01 GMT';
		document.cookie='facebook_uid=;expires=Thu, 01-Jan-1970 00:00:01 GMT';
		document.logout.submit();
	}
	</script> -->
	<p>Ready to sync?  Click the blue box below:</p>
	<p>
		<a href=\"http://www.facebook.com/login.php?api_key=".$config['api_key']."&next=".$config['next']."&v=1.0\"><img src=\"http://static.ak.facebook.com/images/devsite/facebook_login.gif\" border=\"0\"></a>
	</p>
	<p style='font-size: 11px;'>
		fl2fb is built by <a href='http://www.bram.us'>Bramus!</a> and was based upon <a href='http://www.mononexo.com/flickurbook/'>Flickurbook</a>, built by Matt McNamara (mcnamamj<i><b><u>at</u></b></i>notes.udayton DOT edu).
	</p>";

	die();


}
?>
<html>
<head>
<title>fl2fb - One way photo album syncing between Flickr and Facebook</title>
<style type="text/css">
.title		 {
	font-family: Verdana;
	font-size: 22px;
	font-style: italic;
	font-weight: 800;
	letter-spacing: 3px;
}
.heading	{
	font-family: Verdana;
	font-size: 16px;
	text-decoration: none;
}
body		{
	font-family: "Verdana";
}
.teaser 	{
		padding:0 0 0 11px;
		margin:10px;
		background:url("tsr-l.gif") top left no-repeat; 
}
.teaser h3 	{
		background:url("tsr-r.gif") top right no-repeat;
		margin:0;
		padding:8px 0 0.5em 0;
}
.teaser p 	{
		margin:0 0 0 -11px;
		padding:0 11px 0.5em;
		border:1px solid #d8d8d8;
		border-width:0 1px;
		background:#fff;
}	
.teaser div	{
		margin:0 0 0 -11px;
		padding-left:11px;
		background:url("tsr-l.gif") bottom left no-repeat;
		background-repeat: no-repeat;
}
.teaser a.more	{
		display:block;
		text-align:right;
		padding:0 10px 10px 0;
		background:url("tsr-r.gif") bottom right no-repeat;
		background-repeat: no-repeat;
}
</style>
<script>
page_loading = true;

function process(x,a) {
	if(page_loading)
		alert("You must wait until this page finishes loading.");
	else {
		document.all.processframe.src="internal.php?x="+ x + "&a=" + a;
	    document.all.processframe.style.display="inline";
	    document.getElementById("albums").style.display="none";
	    document.getElementById("working").style.display="block";
	}
}

function loaded() {
	page_loading = false;	
}

function done() {
	document.getElementById("albums").style.display="block";
    document.getElementById("working").style.display="none";
}
</script>
</head>
<body onload="loaded();" >
<p align="center">
<span class="title">fl2fb</span><br>
<span class="heading">One way photo album syncing between Flickr and Facebook</span></p>
<p>&nbsp;</p>
<p align="center"><form action="" method="POST"><b>Enter a flickr username:</b> <input name="username" value="<?php echo $username; ?>"></form></p>
<?
function footer()
{
	print '<p>&nbsp;</p>
	<hr><center><small>built by <a href="http://www.bram.us/">Bramus!</a>, based upon <a href="http://www.mononexo.com/flickurbook/">Flickurbook</a> by Matt McNamara';
	print '</small>
	</center>
	</body>
	</html>';
	die();
}
if($username === '')
{
	footer();
}

if(get_magic_quotes_gpc())
{
	$username = stripslashes($username);
}
require_once("../library/phpFlickr/phpFlickr.php");
$f = new phpFlickr($config['flickr'], null, true);
$id = "http://www.flickr.com/people/$username/";
$id = $f->urls_lookupUser($id);
if(!$id)
{
	echo("<p style=\"color:maroon;\">Error: invalid ID ($id)</p>");
	footer();
}
$photosets= $f->photosets_getList($id['id']);
if(!$photosets)
{
	echo "<p style=\"color:maroon;\">Error in getting photosets.</p>";
	footer();
}
flush();
echo "<div id=\"working\" style=\"display:none\"><h3>Working on your request...</h3></div>\n";
echo "<div id=\"albums\"><p>Here are the available photo sets from $username (limited to the first 10):</p>\n";
echo "<br><ul>\n";
$count = count($photosets['photoset']);

for($i = 0;$i < $count && $i < 10;++$i)
{
	$thisSet = $photosets['photoset'][$i];
	$photoInfo = $f->photos_getInfo($thisSet['primary'], $thisSet['secret']);
	$photoInfo = "http://farm" . $photoInfo['farm'] . ".static.flickr.com/" . $photoInfo['server'] . "/" . $photoInfo['id'] . "_" . $photoInfo['secret'] . "_m.jpg";
	
	echo "<div class=\"teaser\">
		<h3>Photoset Title: <u>" . $thisSet['title'] . "</u></h3>
		
		<p><img src=\"$photoInfo\" style=\"float: left;\"><b>View the photos in this set</b>: <a href=\"http://www.flickr.com/photos/$username/".$thisSet['primary']."/in/set-".$thisSet['id']."/\" target=_new>Click here</a></p>";
	echo "<p>&nbsp;</p><p><b>Description</b>: ";
	if($thisSet['description'] == "")
		echo "<i><small>no description set</small></i>\n";
	else 
		echo $thisSet['description'];
	echo "</p>";
	echo "<p>&nbsp;</p><p><b>Number of photos:</b>: " . $thisSet['photos'] . "</p>\n";
	echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><div><a href=\"javascript:process('" . $thisSet['id'] . "', '" . $thisSet['title'] . "');\" class=\"more\">Send this photoset to Facebook</a></div></div><p>&nbsp;</p>\n";		
	flush();
}
echo "</ul></div>\n";
?>
<iframe id="processframe"  frameborder="0"  vspace="0"  hspace="0"  marginwidth="0"  marginheight="0"
                  width="97%"  scrolling="yes"  height="500"  style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; Z-INDEX: 999; LEFT: 20px; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid;">
</iframe>
<p>&nbsp;</p>
<p>&nbsp;</p>
<?php footer(); ?>
</body>
</html>